﻿using System;
using System.Collections.Generic;
using System.Drawing;
using MagistrAPI.Enums;
using MagistrAPI.MathModel;
using ZedGraph;
using ZedGraphControl;

namespace MagistrWPFUI.Presenter
{
    internal class ZedGraphGraphic:IGraphic
    {
        private Dictionary<Equations, List<double>> _rezults;
        private static ZedGraphUserControl _zgc;

        public ZedGraphGraphic(Dictionary<Equations, List<double>> rezults, ZedGraphUserControl zgc)
        {
            _rezults = rezults;
            if (_zgc == null)
            {
                _zgc = zgc;
            }
        }

        public void Draw(Characteristics characteristic)
        {
            if (_rezults == null)
            {
                throw new Exception("Не удалось проинтегрировать");
            }
            int n = _rezults[Equations.T].Count;
            GraphPane graphic = _zgc.Zgc.GraphPane;
            PointPairList xy = new PointPairList();
            for (int i = 0; i < n; i+=10)
            {
                double[] y = GetYFromRezult(i);
                var characteristicEquation = (Func<double[], double>) CharacteristicEquations.Characteristics[characteristic];
                xy.Add(_rezults[Equations.T][i], characteristicEquation(y));
            }
            graphic.CurveList.Clear();
            graphic.AddCurve(characteristic.ToString(), xy, Color.Black, SymbolType.None);
        }

        private double[] GetYFromRezult(int i)
        {
            return new[]
                {
                    _rezults[Equations.X][i],
                    _rezults[Equations.Y][i],
                    _rezults[Equations.Z][i],
                    _rezults[Equations.Vx][i],
                    _rezults[Equations.Vy][i],
                    _rezults[Equations.Vz][i],
                    _rezults[Equations.Wx][i],
                    _rezults[Equations.Wy][i],
                    _rezults[Equations.Wz][i],
                    _rezults[Equations.Lx][i],
                    _rezults[Equations.Ly][i],
                    _rezults[Equations.Lz][i],
                    _rezults[Equations.Mx][i],
                    _rezults[Equations.My][i],
                    _rezults[Equations.Mz][i],
                    _rezults[Equations.Nx][i],
                    _rezults[Equations.Ny][i],
                    _rezults[Equations.Nz][i]
                };
        }
    }
}
